<?php

namespace addons\crontab\controller;

use addons\crontab\model\Crontab;
use app\admin\model\inventory\Kc;
use Cron\CronExpression;
use fast\Http;
use think\Controller;
use think\Db;
use think\Exception;
use think\Log;

/**
 * 定时任务接口
 *
 * 以Crontab方式每分钟定时执行,且只可以Cli方式运行
 * @internal
 */
class Rdrecord extends Controller
{

    /**
     * 初始化方法,最前且始终执行
     */
    public function _initialize()
    {
        // 只可以以cli方式执行
        if (!$this->request->isCli()) {
            $this->error('Autotask script only work at client!');
        }

        parent::_initialize();

        // 清除错误
        error_reporting(0);

        // 设置永不超时
        set_time_limit(0);
    }

    /**
     * 执行定时任务
     */
    public function index()
    {
        $data  = [
            'header' => [
                'code' => 'IC002230600005',
                'order_date' => '2023-06-05',
                'wh_code' => '003',
                'shelves_code'=>'002B520305',
                'createtime' => '2023-6-3 23:59:59',
                'sh_time' => '2023-6-3 23:59:59',
                'applyer'=>'goal',
            ],
            'item'=>[[
                         'quantity'=>'1',
                         'quantity2'=>'1',
                         'inv_code'=>'010101',
                         'free'=>'',
                         'unit_code'=>'只',
                         'unit_code2'=>'只'
                     ],
                     [
                         'quantity'=>'1',
                         'quantity2'=>'1',
                         'inv_code'=>'010202',
                         'free'=>'',
                         'unit_code'=>'只',
                         'unit_code2'=>'只'
                     ]
            ],
        ];



        $header = $data['header'];
        $item   = $data['item'];
        $warehouseCode = $header['wh_code'];
        $shelvesCode  = $header['shelves_code'];

        $warehouseId = Db::name('warehouse')->where('number',$warehouseCode)->value('id');
        $shelvesId = Db::name('shelves')->where('number',$shelvesCode)->value('id');


        Db::startTrans();
        try {
            //入库
            foreach ($item as $k=>$v){
                //更新本地库存
                Kc::rdRecordUpdateKc($warehouseCode,$header['createtime'],$v['inv_code'],$v['quantity'],$header['code'],$v['order_date'],1,5);
                //插入中间库
                $list[] = [
                    'cInvCode'=>$v['inv_code'],
                    'iQuantity'=>$v['quantity'],
                    'createtime'=>$header['createtime']
                ];
            }
            $this->insertMiddlData($warehouseCode,$header['order_date'],$header['code'],$list,$middleType);
            Db::commit();
        } catch (Exception $e) {

            Db::rollback();
            $this->error('处理失败,' . $e->getMessage());
        }
        return;
    }

    /**
     * 更新第三方库
     * @param $cWhCode
     * @param $dDate
     * @param $cCode
     * @param $list
     * @param $type
     * @return void
     * @throws Exception
     */
    public function insertMiddlData($cWhCode,$dDate,$cCode,$list,$type = 'in')
    {
        if ($type == 'in') {
            $table  = 'RdRecord08';
            $tables = 'rdrecords08';
        } elseif ($type == 'out') {
            $table  = 'RdRecord09';
            $tables = 'rdrecords09';
        }
        //插入中间库
        $id = time();
         Db::connect('database.inc')->table($table)->insertGetId([
            'bRdFlag'=>1,
            'cVouchType' =>'08' ,
            'cSource' => '库存',
            'cWhCode' => $cWhCode,
            'dDate'=>$dDate,
            'cCode'=>$cCode,
            'bIsSTQc'=>1,
            'bTransFlag'=>1,
            'ID'=>$id,
        ]);
         $i = 0;
        foreach ($list as $k=>$v){
            $i++;
            Db::connect('database.inc')->table($tables)->insert([
                'cInvCode'=>$v['cInvCode'],
                'ID' => $id,
                'AutoID'=>time()+$i,
                'iQuantity' => $v['iQuantity'],
                'iFlag' => 0,
            ]);
        }
    }

}
